async function runSample() {
  const path = require("path");
  const uuid = require("uuid");
  const dialogflow = require("dialogflow").v2beta1;
  const sessionClient = new dialogflow.SessionsClient({
    keyFilename: path.join(__dirname, "./GrantDialogflowNode-02f1c9fcdbb6.json")
  });
  const projectId = "grantdialogflownode-fwoaqv";
  const sessionId = uuid.v4();
  const languageCode = "en-US";
  const knowledgeBaseFullName = `Grantknowledge`;
  const query = `How do I protect myself from accidental data deletion?`;

  const sessionPath = sessionClient.sessionPath(projectId, sessionId);
  const knowbase = new dialogflow.KnowledgeBasesClient();
  const knowledgeBasePath = knowbase.knowledgeBasePath(
    projectId,
    knowledgeBaseFullName
  );

  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        text: query,
        languageCode: languageCode
      }
    },
    queryParams: {
      knowledgeBaseNames: [knowledgeBasePath]
    }
  };

  const responses = await sessionClient.detectIntent(request);
  const result = responses[0].queryResult;
  console.log(`Query text: ${result.queryText}`);
  console.log(`Detected Intent: ${result.intent.displayName}`);
  console.log(`Confidence: ${result.intentDetectionConfidence}`);
  console.log(`Query Result: ${result.fulfillmentText}`);
  const answers = result.knowledgeAnswers.answers;
  console.log(`There are ${answers.length} answer(s);`);
  answers.forEach(a => {
    console.log(`   answer: ${a.answer}`);
    console.log(`   confidence: ${a.matchConfidence}`);
    console.log(`   match confidence level: ${a.matchConfidenceLevel}`);
  });
}

setTimeout(() => {
  runSample();
}, 1200);
